//
//  ViewController04.swift
//  APPTestSwift
//
//  Created by hiking on 2020/2/20.
//  Copyright © 2020 hiking. All rights reserved.
//

import UIKit

let VC04CellID = "Cell"
let HeaderHeight = 250  //注意首字母要大写
let TabBarHeight = 50
class ViewController04: UIViewController{
    
    lazy var mainScrollView:UIScrollView = {
        let scrollView = UIScrollView.init(frame: CGRect.init(x: 0, y: 50, width: viewW, height: viewH))
        scrollView.contentSize = CGSize.init(width: self.viewW, height: HeaderHeight + self.viewH)
        scrollView.delegate = self
        return scrollView
    }()
    
    lazy var headerView:UIView = {
        let header = UIView.init(frame: CGRect.init(x: 0, y: 0, width: self.viewW, height: HeaderHeight))
        header.backgroundColor = .green
        return header
    }()
    lazy var tabBarView:UIView = {
        let tabBar = UIView.init(frame: CGRect.init(x: 0, y: HeaderHeight, width: self.viewW, height: TabBarHeight))
        tabBar.backgroundColor = .red
        return tabBar
    }()
    lazy var childScrollView:UIScrollView = {
        let scrollView = UIScrollView.init(frame: CGRect.init(x: 0, y: HeaderHeight + TabBarHeight, width: viewW, height: self.viewH - TabBarHeight))
        scrollView.contentSize = CGSize.init(width: self.viewW * 2, height:  self.viewH - TabBarHeight)
        scrollView.delegate = self
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.isPagingEnabled = true
        return scrollView
    }()
    lazy var tableView:UITableView = {
        let tableView = UITableView.init(frame: CGRect.init(x: 0, y: 0, width: self.viewW, height: self.viewH - TabBarHeight), style: .plain)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: VC04CellID)
        return tableView
    }()
    
    lazy var tableView2:UITableView = {
           let tableView = UITableView.init(frame: CGRect.init(x: self.viewW, y: 0, width: self.viewW, height: self.viewH - TabBarHeight), style: .plain)
           tableView.dataSource = self
           tableView.delegate = self
           tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: VC04CellID)
           return tableView
    }()
    
    var viewH:Int = 0
    var viewW:Int = 0
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        viewH = (Int)(self.view.frame.size.height) - 50
        viewW = (Int)(self.view.frame.size.width)
        setUpUI()
    }
    
    func setUpUI() {
        self.view.addSubview(self.mainScrollView)
        self.mainScrollView.addSubview(self.headerView)
        self.mainScrollView.addSubview(self.tabBarView)
        self.mainScrollView.addSubview(self.childScrollView)
        self.childScrollView.addSubview(self.tableView)
        self.childScrollView.addSubview(self.tableView2)
        
    }

}
//tableView代理
extension ViewController04:UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 20
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: VC04CellID,for: indexPath)
        cell.textLabel?.text = "第\(indexPath.row + 1)行"
        return cell
    }
}
//UIScrollView 代理
extension ViewController04:UIScrollViewDelegate{
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        //先判断当前scrollView是主scrollView还是子
        let isContent = (scrollView == self.tableView || scrollView == self.tableView2)
        
        if isContent {
            print("\(scrollView.contentOffset.y) tableView")
            //如果mainScrollView的offsetY小于tabBar的y,就可以滑动
            let mainCanScroll = self.mainScrollView.contentOffset.y < self.tabBarView.frame.origin.y
            let offsetY = scrollView.contentOffset.y + self.mainScrollView.contentOffset.y
            if mainCanScroll {
                self.mainScrollView.setContentOffset(CGPoint.init(x: 0, y: offsetY), animated: false)
                scrollView.setContentOffset(.zero, animated: false)
            }else if scrollView.contentOffset.y <= 0 && mainCanScroll == false {
                if self.mainScrollView.contentOffset.y >= self.tabBarView.frame.origin.y {
                    self.mainScrollView.setContentOffset(CGPoint.init(x: 0, y: offsetY), animated: false)
                }
            }
        } else if scrollView == self.mainScrollView{

            if self.mainScrollView.contentOffset.y >= self.tabBarView.frame.origin.y{
                self.mainScrollView.setContentOffset(CGPoint.init(x: 0, y: self.tabBarView.frame.origin.y), animated: false)
            }
        }
    }
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        if scrollView == self.tableView {
            if self.mainScrollView.contentOffset.y < 0{
                self.mainScrollView.setContentOffset(.zero, animated: true)
            }
        }
    }
}
